﻿using Core;
using DataAccess;
using KhoaLuan.Utils;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.ProcessAJAX
{
    public partial class AjaxOrg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string GetListOrg(int currentpage, int numberinpage, string keyword)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementOrg.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.View))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xem danh sách các tổ chức" });
            try
            {
                tbOrg_Proxy pr = new tbOrg_Proxy();
                List<DataAccess.tbOrg> list = new List<DataAccess.tbOrg>();
                int totalitem = 0;
                if (keyword == "null" || keyword == "")
                {
                    list = pr.SelectAlltbOrg_Proxy();
                    totalitem = list.Count;
                }
                else
                {
                    list = (pr.SelectNo_tbOrg_Proxy(keyword)).ToList<DataAccess.tbOrg>();
                }

                totalitem = list.Count;
                var list2 = list.Skip((currentpage - 1) * numberinpage).Take(numberinpage);
                

                return new JavaScriptSerializer().Serialize(new { Status = true, Data = list2, TotalItem = totalitem, CurrentPage = currentpage });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false, Data = "" });
            }
        }
        [WebMethod]
        public static string DeleteOrg(int OrgID)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementOrg.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Delete))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xóa tổ chức" });
            try
            {
                tbOrg_Proxy pr = new tbOrg_Proxy();
                if (pr.Delete_tbOrg_Proxy(OrgID))
                    return new JavaScriptSerializer().Serialize(new { Status = true });
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }
        [WebMethod]
        public static string Insert_tbOrg(string NameOrg, int TypeOrg, string Description)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementOrg.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.AddNew))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền thêm tổ chức" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbUsers).Name);
                tbOrg_Proxy pr = new tbOrg_Proxy();
                tbOrg user = new tbOrg();
                user.OrgID = 0;
                user.NameOrg = ConvertType.ToString(NameOrg);
                user.TypeOrg = ConvertType.ToInt(TypeOrg);
                user.Description = ConvertType.ToString(Description);

                if (pr.Insert_tbOrg_Proxy(user))
                    log.Info(UserLogin.GetUserName + "Thêm mới Org  thành công");
                return new JavaScriptSerializer().Serialize(new { Status = true });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }
        [WebMethod]
        public static string UpdatetbOrg(int OrgID, string NameOrg, int TypeOrg, string Description)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementOrg.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Edit))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền chỉnh sửa tổ chức" });
            ILog log = LogManager.GetLogger(typeof(DataAccess.tbUsers).Name);
            tbOrg_Proxy pr = new tbOrg_Proxy();
            tbOrg user = new tbOrg();
            user.OrgID = ConvertType.ToInt(OrgID);
            user.NameOrg = ConvertType.ToString(NameOrg);
            user.TypeOrg = ConvertType.ToInt(TypeOrg);
            user.Description = ConvertType.ToString(Description);
            try
            {

                if (pr.Update_tbOrg_Proxy(user))
                    return new JavaScriptSerializer().Serialize(new { Status = true });
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }

    }
}